package com.sky.financelake.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sky.financelake.R;
import com.sky.financelake.bean.dto.StockDto;
import com.sky.financelake.enums.StockToolEnums;
import com.sky.financelake.ui.wigets.FlowTagLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CombinationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final String TAG = CombinationAdapter.class.getCanonicalName();

    private final List<String> combinations;
    private final Context mContext;

    public CombinationAdapter(List<String> combinations, Context mContext) {
        this.combinations = combinations;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_combination_tool, parent, false);
        return new CombinationViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof CombinationViewHolder) {
            String tag = combinations.get(position);
            String[] tags = tag.split(",");
            List<String> values = new ArrayList<>();
            for (String name: tags) {
                values.add(StockToolEnums.valueOf(name).getName());
            }
            ((CombinationViewHolder) holder).flowTagLayout.addTags(values);
        }
    }

    @Override
    public int getItemCount() {
        return combinations.size();
    }

    public interface OnItemClickListener{
        void onClick(int position);
    }
    public OnItemClickListener listener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.listener = onItemClickListener;
    }

    public class CombinationViewHolder extends RecyclerView.ViewHolder {

        FlowTagLayout flowTagLayout;

        public CombinationViewHolder(@NonNull View itemView) {
            super(itemView);
            flowTagLayout = itemView.findViewById(R.id.ftl_combination);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener!=null){
                        listener.onClick(getAdapterPosition());
                    }
                }
            });
        }
    }
}
